﻿using Sw.Hospital.HealthExaminationSystem.Common.Bases;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sw.Hospital.HealthExaminationSystem.Application.PersonnelCategorys;
using Sw.Hospital.HealthExaminationSystem.Application.PersonnelCategorys.Dto;
using Sw.Hospital.HealthExaminationSystem.Common.GlobalSources;
using Sw.Hospital.HealthExaminationSystem.Application.SummarizeAdvice;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.SummarizeAdvice;
using Sw.Hospital.HealthExaminationSystem.Application.SummarizeAdvice.Dto;

namespace Sw.Hospital.HealthExaminationSystem.UserSettings.PersonnelCategory
{
    public partial class SummarizeAdvice : UserBaseForm
    {
        private ISummarizeAdviceAppService _summarizeAdviceAppService;

        SummarizeAdviceDto perCate = new SummarizeAdviceDto();

        List<SummarizeAdviceDto> listAdvices = new List<SummarizeAdviceDto>();
        public SummarizeAdvice()
        {
            InitializeComponent();
            _summarizeAdviceAppService = new SummarizeAdviceAppService();
        }

        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCategory_Load(object sender, EventArgs e)
        {
            listAdvices = _summarizeAdviceAppService.GetAllSummAll();// _personnelCategoryAppService.QueryCategoryList(new PersonnelCategoryViewDto());
            gridControl.DataSource = listAdvices;
        }

        private void RepositoryItemButtonEdit1_Click(object sender, System.EventArgs e)
        {
            throw new System.NotImplementedException();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtAiAdvice.Text))
            {
                dxErrorProvider.SetError(txtAiAdvice, string.Format(Variables.MandatoryTips, "建议内容"));
                txtAiAdvice.Focus();
                return;
            }
            var rowList = gridViewPersonnelCategoryViewDto.GetSelectedRows();
            var row = gridViewPersonnelCategoryViewDto.GetRow(rowList[0]) as SummarizeAdviceDto;
            row.DepartmentAdvice = row.HealthcareAdvice = row.SummAdvice = txtAiAdvice.Text.Trim();

            SummarizeAdviceInput summarizeAdviceInput = new SummarizeAdviceInput()
            {
                  SummarizeAdvice = new CreateOrUpdateSummarizeAdviceDto()
                  {
                       Id = row.Id,
                       DepartmentAdvice = txtAiAdvice.Text,
                       HealthcareAdvice = txtAiAdvice.Text,
                       SummAdvice = txtAiAdvice.Text
                  }
            };

            _summarizeAdviceAppService.Edit(summarizeAdviceInput);
            FrmCategory_Load(null, null);
        }

        //点击行编辑
        private void gdvCategory_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2 && e.RowHandle >= 0)
            {
                AutoLoading(() =>
                {
                    var rowList = gridViewPersonnelCategoryViewDto.GetSelectedRows();
                    var row = gridViewPersonnelCategoryViewDto.GetRow(rowList[0]) as SummarizeAdviceDto;

                    perCate = row;
                    var adviceName = perCate.AdviceName;
                    txtAiAdvice.Text = AI.AiPost.AiPostData(adviceName);
                },"AI建议生成中...");
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtSearchName.Text))
            {
                var cate = gridControl.DataSource as List<SummarizeAdviceDto>;
                cate = cate.Where(o => o.AdviceName.Contains(txtSearchName.Text)).ToList();
                gridControl.DataSource = cate;
                gridControl.RefreshDataSource();
            }
            else
            {
                gridControl.DataSource = listAdvices;
                gridControl.RefreshDataSource();
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtAiAdvice.Text))
            {
                dxErrorProvider.SetError(txtAiAdvice, string.Format(Variables.MandatoryTips, "类别名称"));
                txtAiAdvice.Focus();
                return;
            }          
           
        }

        private void txtAiAdvice_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
